package com.itoffer.web;

import com.itoffer.pojo.Applicant;
import com.itoffer.pojo.ResumeBasicInfo;
import com.itoffer.service.ResumeService;
import com.itoffer.service.impl.ResumeServiceImpl;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/resume/*")
public class ResumeServlet extends BaseServlet{
    private ResumeService resumeService = new ResumeServiceImpl();

    /**
     * 查询数据，查询简历个人信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void selectCurrentResumeServlet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        //通过applicant_id查询当前对象所持有的basicinfo_id
        //获取session对象中的applicant
        HttpSession session = req.getSession();
        Applicant applicant =(Applicant)session.getAttribute("applicant");

        //测试
        //System.out.println(applicant);

        int applicant_id = applicant.getApplicant_id();

        //调用业务层方法来查询
        Integer basicinfo_id = resumeService.selectBasicinfo_idByApplicant_id(applicant_id);
        System.out.println(basicinfo_id);
        if(basicinfo_id==null){
            resp.sendRedirect("/itoffer/applicant/resumeBasicInfoAdd.html");
        }

        //调用查询资料的方法
        ResumeBasicInfo basicInfo = resumeService.selectCurrentResume(basicinfo_id);

        //将简历信息存入session,以便修改密码或者修改头像时用到
        HttpSession session_basicInfo = req.getSession();
        session_basicInfo.setAttribute("basicInfo",basicInfo);

        //测试
        //System.out.println(basicInfo);

        if(basicInfo!=null){
            req.setAttribute("basicInfo",basicInfo);
            req.getRequestDispatcher("/applicant/resume.jsp").forward(req,resp);
        }else{
            //如果查无此人，先暂时让它为空，以后修改
            //12月6号已修改，修改为先判断数据库中是否存放有简历信息，没有简历信息先填简历，有简历显示简历信息，具体方法在selectCurrentResumeByApplicant_id
            String contextPath = req.getContextPath();
            resp.sendRedirect(contextPath+"/error.jsp");
        }

    }

    /**
     * 根据session中的applicant_id查询数据库中有无简历信息
     * @param req
     * @param resp
     * @description 若有简历信息，直接跳转到简历，若没有简历信息，跳转到添加简历信息界面
     * @throws ServletException
     * @throws IOException
     */
    public void selectCurrentResumeByApplicant_id(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        HttpSession session = req.getSession();
        Applicant applicant = (Applicant)session.getAttribute("applicant");
        int applicant_id  = applicant.getApplicant_id();

        //测试
        System.out.println(applicant_id);

        //调用业务层方法查询
        boolean b = resumeService.selectResumeByApplicant_id(applicant_id);
        String contextPath = req.getContextPath();
        if(b){

            resp.sendRedirect(contextPath+"/resume/selectCurrentResumeServlet");
        }else{
            //没有填写过简历，跳转到添加简历页面
            resp.sendRedirect(contextPath+"/applicant/resumeBasicInfoAdd.html");
        }
    }
    /**
     * 回显数据在修改界面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void selectCurrentResumeServlet2(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        //通过applicant_id查询当前对象所持有的basicinfo_id
        //获取session对象中的applicant
        HttpSession session = req.getSession();
        Applicant applicant =(Applicant)session.getAttribute("applicant");

        //测试
        //System.out.println(applicant);

        int applicant_id = applicant.getApplicant_id();

        //调用业务层方法来查询
        int basicinfo_id = resumeService.selectBasicinfo_idByApplicant_id(applicant_id);

        //调用查询资料的方法
        ResumeBasicInfo basicInfo = resumeService.selectCurrentResume(basicinfo_id);

        //测试
        System.out.println(basicInfo);

        if(basicInfo!=null){
            req.setAttribute("basicInfo",basicInfo);
            req.getRequestDispatcher("/applicant/resumeBasicinfoUpdate.jsp").forward(req,resp);
        }else{
            //如果查无此人，先暂时让它为空，以后修改
            String contextPath = req.getContextPath();
            resp.sendRedirect(contextPath+"/error.jsp");
        }
    }

    /**
     * 更新简历信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void updateResumeServlet(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;chart = utf-8");
        resp.setCharacterEncoding("utf-8");
        int basicinfo_id = Integer.parseInt(req.getParameter("basicinfo_id"));
        String realname = req.getParameter("realname");
        String gender = req.getParameter("gender");
        String birthday = req.getParameter("birthday");
        String telephone = req.getParameter("telephone");
        String email = req.getParameter("email");
        String job_intension = req.getParameter("job_intension");
        String job_experience = req.getParameter("job_experience");

        //封装对象
        //创建一个Resume对象
        ResumeBasicInfo r = new ResumeBasicInfo();
        r.setBasicinfo_id(basicinfo_id);
        r.setRealname(realname);
        r.setGender(gender);
        r.setBirthday(birthday);
        r.setTelephone(telephone);
        r.setEmail(email);
        r.setJob_intension(job_intension);
        r.setJob_experience(job_experience);

        //调用业务层修改方法来进行修改
        boolean b = resumeService.updateResume(r);

        //再查询出来刷新后的值
        ResumeBasicInfo basicInfo = resumeService.selectCurrentResume(basicinfo_id);
        if(b){
            HttpSession session = req.getSession();
            session.setAttribute("basicInfo",basicInfo);
            String contextPath = req.getContextPath();
            resp.sendRedirect(contextPath+"/applicant/resume.jsp");
        }else {
            //修改失败，跳转到错误页面
            String contextPath = req.getContextPath();
            resp.sendRedirect(contextPath+"/error.jsp");
        }

    }

    /**
     * 简历预览
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void viewResumeServlet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        HttpSession session = req.getSession();
        ResumeBasicInfo resume =(ResumeBasicInfo)session.getAttribute("basicInfo");
        req.setAttribute("resume",resume);
        req.getRequestDispatcher("/applicant/resumeView.jsp").forward(req,resp);
    }
}
